// JavaScript Document
(function ($) {
    $.fn.slide = function (options) {
        var opts = $.extend({}, $.fn.slide.defaults, options);
        var index = 0;
        var autoPlay;
        var claPre = $("." + opts.preCla);
        var claNext = $("." + opts.nextCla);
        var contentBox = $(this);
        var slideW = contentBox.children().first().innerWidth();
        var slideH = contentBox.children().first().innerHeight();
        var slideWH;
        var contentBoxNum = $(this).children().size();
        if (opts.effect == "scrollLeft" || opts.effect == "scrollRight") {
            contentBox.css("width", contentBoxNum * slideW);
            slideWH = slideW;
        } else {
            slideWH = slideH;
        }
        if (contentBoxNum > opts.minNumber) {
            return this.each(function () {
                var $this = $(this);
                var doPlay = function () {
                    $.fn.slide.effect[opts.effect]($this, index, slideWH, opts);
                    index++;
                    if (index * opts.steps >= contentBoxNum) {
                        index = 0;
                    }
                }
                if (opts.autoPlay) {
                    autoPlay = setInterval(doPlay, opts.timer);
                    $this.hover(function () {
                        if (autoPlay) {
                            clearInterval(autoPlay);
                        }
                    }, function () {
                        if (autoPlay) {
                            clearInterval(autoPlay);
                        }
                        autoPlay = setInterval(doPlay, opts.timer);
                    });
                }
                claPre.click(function () {
                    if (autoPlay) {
                        clearInterval(autoPlay);
                    }
                    $.fn.slide.effect["scrollRight"]($this, index, slideWH, opts);
                    index++;
                    //doPlay();
                    if (opts.autoPlay) {
                        autoPlay = setInterval(doPlay, opts.timer);
                    }
                });
                claNext.click(function () {
                    if (autoPlay) {
                        clearInterval(autoPlay);
                    }
                    index -= 2;
                    doPlay();
                    if (opts.autoPlay) {
                        autoPlay = setInterval(doPlay, opts.timer);
                    }
                });

            });
        }
    }
    $.fn.slide.defaults = {
        effect: "fade",
        timer: 3000,
        speed: 1000,
        steps: 1,
        autoPlay: true,
        minNumber: 1,
        preCla: "pre",
        nextCla: "next"
    }
    $.fn.slide.effect = {
        fade: function (contentObj, i, slideW, opts) {
            contentObj.children().fadeOut();
            contentObj.children().eq(i).stop().fadeIn(opts.speed);
        },
        scrollLeft: function (contentObj, i, slideW, opts) {
            contentObj.stop().animate({ "marginLeft": -opts.steps * slideW }, opts.speed, function () {
                for (var index = 0; index < opts.steps; index++) {
                    contentObj.children().eq(0).appendTo(contentObj);
                }
                contentObj.css({ "marginLeft": 0 });
            });
        },
        scrollRight: function (contentObj, i, slideW, opts) {
            contentObj.stop();
            for (var index = contentObj.children().length - 1; index > contentObj.children().length - opts.steps - 1; index--) {
                contentObj.children().eq(index).prependTo(contentObj);
            }
            contentObj.css({ "marginLeft": -opts.steps * slideW });
            contentObj.animate({ "marginLeft": 0 }, opts.speed);

        },
        scrollUp: function (contentObj, i, slidH, opts) {
            contentObj.stop().animate({ "marginTop": -opts.steps * slidH }, opts.speed, function () {
                for (var index = 0; index < opts.steps; index++) {
                    contentObj.children().eq(index).appendTo(contentObj);
                }
                contentObj.css({ "marginTop": 0 });
            });
        },
        scrollDown: function () {
        }
    }
})(jQuery)